(function(window, undefined) {
	var dino  =window.dino = window. dino || {};


	// 工厂
	// 创建精灵
	dino.create = function(type, opts) {
		var dic = {
			"tree": function(opts) {
				var info=dino.infoDic["tree"];
				var obj = new staticObj({
					size: info.size,
					filename:info.textureUrl,
					rect: cc.RectMake(0, 0, info.size.width, info.size.height)
				});


				obj.attach('afterStatusChange', function(opts) {
					if(opts.nstat == 'create') {
						var animation = dino.getSkin('tree', 'create');
						var action = cc.Animate.create(animation);
						var rpt = cc.RepeatForever.create(action);
						this.runAction(rpt);
						this.changeStatus("normal");
					} else if(opts.nstat == 'normal') {
						var animation = dino.getSkin('tree', 'normal');
						var action = cc.Animate.create(animation);
						var rpt = cc.RepeatForever.create(action);
						this.runAction(rpt);
					}
				}.bind(obj));


				// obj.attach('afterStatusChange', function(opts) {
				// 	if(opts.nstat == 'create') {
				// 		var animation = dino.getSkin('tree', 'create');
				// 		var action = cc.Animate.create(animation);
				// 		var rpt = cc.RepeatForever.create(action);
				// 		this.runAction(rpt);
				// 		console.log("change into 'create' status !!");
				// 		// 
				// 		//this.changeStatus("normal");
				// 	} else if(opts.nstat == 'normal') {
				// 		var animation = dino.getSkin('tree', 'normal');
				// 		var action = cc.Animate.create(animation);
				// 		var rpt = cc.RepeatForever.create(action);
				// 		this.runAction(rpt);
				// 		console.log("change into 'normal' status !!");
				// 	}
				// 	console.log('afterStatusChange', this, opts);
				// }.bind(obj));



				return obj;
			}
		};
		if(dic[type]) {
			return dic[type](opts);
		} else {
			throw "create sprite failed!!";
		}
	}


	// 获取动画皮肤
	dino.getSkin = function(type, skin) {
		var ac = cc.AnimationCache.getInstance();
		var infoDic = dino.infoDic;

		var key = [type, skin].join('.');
		var rst;
		while(!(rst = ac.getAnimation(key))) {
			// 图片地址
			var ak = infoDic[type]['animDic'];
			var imgUrl = ak[skin].textureUrl ||  infoDic[type].textureUrl; // 如果没有专门的skin贴图,则使用默认的贴图
			var steps = ak[skin].steps;
			var size = ak.size||infoDic[type].size;
			if(!imgUrl) {
				break;
			}
			// 贴图
			var tc = cc.TextureCache.getInstance();
			var texture = tc.addImage(imgUrl);
			// 帧
			var fc = cc.SpriteFrameCache.getInstance();
			var spriteFrames = [];
			for(var i = 0; i < steps.length; i++) {
				var si = steps[i];
				var frame = cc.SpriteFrame.createWithTexture(texture, new cc.Rect(size.width * si[1], size.height * si[0], size.width, size.height));
				spriteFrames.push(frame);
			}
			// 帧间隔
			var delay = dino.config.animationDelay;
			// 动画
			var anim = cc.Animation.create(spriteFrames, delay);
			// 加入动画缓存
			ac.addAnimation(anim, key);
		}
		return rst;
	};



	// config
	dino.config = {
		// 动画间隔
		animationDelay: .2
	};

})(window);